package cn.ekuma.epos.datalogic.define.dao.crm.voucher;

import java.util.Date;
import java.util.List;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.crm.voucher.I_Voucher;

import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.crm.voucher.Voucher;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class VoucherDAO extends ModifiedLogDAO<Voucher>{

    public VoucherDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                    "VOUCHER",
                    new Field[]{
                       new Field(I_Voucher.ID,Datas.STRING, Formats.STRING),
                       new Field(I_Voucher.CUSTOMER_ID,Datas.STRING, Formats.STRING,BusinessPartner.class),
                       new Field(I_Voucher.CREATEDATE,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_Voucher.STATE,Datas.INT, Formats.INT),
                       new Field(I_Voucher.VALIDITYDATE,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_Voucher.CURLIMIT,Datas.DOUBLE, Formats.DOUBLE,true),
                       new Field(I_Voucher.MEMO,Datas.STRING, Formats.STRING),
                       new Field(I_Voucher.VIRTUAL,Datas.BOOLEAN, Formats.BOOLEAN),
                       new Field(I_Voucher.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP),
                       new Field(I_Voucher.VOUCHERNUM,Datas.STRING, Formats.STRING),
                       new Field(I_Voucher.VALIDITY_CODE,Datas.STRING, Formats.STRING),
                       new Field(I_Voucher.USEDATE,Datas.TIMESTAMP, Formats.TIMESTAMP)
                     },
                    new int[]{0}
                    );
    }

	@Override
	public void writeInsertValues(DataWrite dp, Voucher obj)
			throws BasicException {
	      dp.setString(1,obj.getId());
	      dp.setString(2,obj.getCustomerId());
	      dp.setTimestamp(3,obj.getCreateDate());
	      dp.setInt(4,obj.getState());
	      dp.setTimestamp(5,obj.getValidityDate());
	      dp.setDouble(6,obj.getCurLimit());
	      dp.setString(7,obj.getMemo());
	      dp.setBoolean(8,obj.isVirtual());
	      dp.setTimestamp(9,obj.getLastModified());
	      dp.setString(10,obj.getVoucherNum());
	      dp.setString(11,obj.getValidityCode());
	      dp.setTimestamp(12,obj.getUseDate());
	}
	
	@Override
	public void writeUpdateRealValues(DataWrite dp, Voucher obj)
			throws BasicException {
		  dp.setString(1,obj.getId());
	      dp.setString(2,obj.getCustomerId());
	      dp.setTimestamp(3,obj.getCreateDate());
	      dp.setInt(4,obj.getState());
	      dp.setTimestamp(5,obj.getValidityDate());
	      //dp.setDouble(6,obj.getCurLimit());
	      dp.setString(6,obj.getMemo());
	      dp.setBoolean(7,obj.isVirtual());
	      dp.setTimestamp(8,obj.getLastModified());
	      dp.setString(9,obj.getVoucherNum());
	      dp.setString(10,obj.getValidityCode());
	      dp.setTimestamp(11,obj.getUseDate());
	}

	@Override
	public Class getSuportClass() {
		return Voucher.class;
	}

	@Override
	public Voucher readValues(DataRead dr,
			Voucher obj) throws BasicException {
		if(obj==null)
			obj=new Voucher();
		obj.setId(dr.getString(1));
		obj.setCustomerId(dr.getString(2));
		obj.setCreateDate(dr.getTimestamp(3));
		obj.setState(dr.getInt(4));
		obj.setValidityDate(dr.getTimestamp(5));
		obj.setCurLimit(dr.getDouble(6));
		obj.setMemo(dr.getString(7));
		obj.setVirtual(dr.getBoolean(8));
		obj.setLastModified(dr.getTimestamp(9));
		obj.setVoucherNum(dr.getString(10));
		obj.setValidityCode(dr.getString(11));
		obj.setUseDate(dr.getTimestamp(12));
		return obj;
	}
	
	@Override
	public List<Voucher> list(Object filter) throws BasicException {
		return new StaticSentence(s, new QBFBuilder(
                this.getTableDefinition().getListSQL()+" WHERE ?(QBF_FILTER) "
                , new String[]{I_Voucher.CREATEDATE, I_Voucher.CREATEDATE, I_Voucher.CUSTOMER_ID}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING}), this).list(filter);
	}
	
	
	public Voucher findVoucherByNum(String cardNum)throws BasicException{
		return (Voucher) new PreparedSentence(s, getTableDefinition().getListSQL()+" WHERE VOUCHERNUM = ? ", SerializerWriteString.INSTANCE, this).find(cardNum);
	}
	
	
	public void changeVoucherState(final String id,final int state)throws BasicException{
		new PreparedSentence(
				s,
				"UPDATE VOUCHER SET STATE = ?,LASTMODIFIED=? WHERE ID = ? ",
				SerializerWriteParams.INSTANCE).exec(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
				setInt(1, state);
				setTimestamp(2,new Date());
				setString(3, id);
			}
		});
	}
}
